package com.cade.agriculturemall.view.personalCenter.selectAddress;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseAdapter;
import com.cade.agriculturemall.base.BaseViewHolder;
import com.cade.agriculturemall.model.AddressEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lobby on 2017/12/27.
 */

public class AddressSelectAdapter extends BaseAdapter {
    private List<AddressEntity> mEntityList = new ArrayList<>();
    private Context mContext;

    public AddressSelectAdapter(Context context) {
        mContext = context;
    }

    @Override
    public void onBindViewHolderWrapper(BaseViewHolder holder, int position) {
        TextView tvContractor = holder.findViewById(R.id.item_address_select_name);
        TextView tvPhone = holder.findViewById(R.id.item_address_select_phone);
        TextView tvAddress = holder.findViewById(R.id.item_address_select_detail);
        RelativeLayout relativeLayout = holder.findViewById(R.id.address_select_rl);

        if (mEntityList.size() != 0) {
            AddressEntity entity = mEntityList.get(position);
            tvContractor.setText(entity.getContractor());
            tvPhone.setText(entity.getMobile());
            String address = "";
            String procincecity = entity.getProvince() + entity.getCity();
            if (entity.getCounty() != null) {
                address += procincecity + entity.getCounty() + entity.getStress() + entity.getAddress();
            } else {
                address += procincecity + entity.getStress() + entity.getAddress();
            }
            if (entity.getIsfirst() == "1") {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(0, 0, 0, 10);//4个参数按顺序分别是左上右下
                relativeLayout.setLayoutParams(layoutParams);

                SpannableString firstAddress = new SpannableString("(默认地址)" + address);
                SpannableStringBuilder spannable = new SpannableStringBuilder(firstAddress);
                spannable.setSpan(new ForegroundColorSpan(Color.RED), 0, 5
                        , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvAddress.setText(spannable);
            } else {
                tvAddress.setText(address);
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(parent, R.layout.item_address_select);
    }

    @Override
    public int getItemCount() {
        return mEntityList.size();
    }

    public void setEntity(List<AddressEntity> mEntity) {
        this.mEntityList = mEntity;
    }

    public List<AddressEntity> getmEntityList() {
        return mEntityList;
    }
}
